package com.pet.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SysPetService;
import com.pet.system.SysPet;
import com.pet.system.SysUser;
import com.pet.utils.Result;
import com.pet.vo.SysPetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 宠物表 前端控制器
 * </p>
 *
 * @author whd
 * @since 2023-08-03
 */
@Api(tags = "宠物接口")

@RestController
@RequestMapping("/lovePet/system/sysPet")
@CrossOrigin("*")
public class SysPetController {
    @Autowired
    SysPetService sysPetService;


    /**
     *
     * @param sysPet
     * @return
     */
    @ApiOperation("添加或修改宠物")
    @GetMapping("AddOrUpdatePet")
    public Result AddOrUpdatePet(@ApiParam(value = "宠物信息") SysPet sysPet) {

        boolean b = sysPetService.saveOrUpdate(sysPet);

        return b?Result.ok():Result.fail();
    }


    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("通过id获取")
    @GetMapping("findById/{id}")
    public Result findById(@ApiParam(value = "宠物ID") @PathVariable Long id) {

        SysPet sysPet = sysPetService.getById(id);

        return sysPet==null?Result.fail():Result.ok(sysPet);
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("删除宠物")
    @GetMapping("deletePetById/{id}")
    public Result deletePetById(@ApiParam(value = "宠物ID") @PathVariable Long id) {

        boolean b = sysPetService.removeById(id);

        return b?Result.ok():Result.fail();
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("宠物列表")
    @GetMapping("getPetList/{id}")
    public Result getPetList(@ApiParam(value = "用户ID") @PathVariable Long id) {

        List<SysPet> list = sysPetService.list(new LambdaQueryWrapper<SysPet>().eq(SysPet::getUserId,id));

        return list==null?Result.fail():Result.ok(list);
    }


    /**
     *
     * @return
     */
    @GetMapping("/findAllPet")
    public Result findAllPet() {
        List<SysPet> list = sysPetService.list();
        return Result.ok(list);
    }


    /**
     *
     * @param page
     * @param limit
     * @param sysPetQueryVo
     * @return
     */
    @ApiOperation("分页和条件查询")
    @PostMapping("{page}/{limit}")
    public Result selectUserPageByVo(
            @ApiParam(value = "当前页数") @PathVariable Long page,
            @ApiParam(value = "每页显示的数量") @PathVariable Long limit,
            @ApiParam(value = "包装的查询分类名称名称") @RequestBody SysPetQueryVo sysPetQueryVo) {
        System.out.println("=========" + sysPetQueryVo.getKeyword());
        IPage<SysPet> iPage = new Page<>(page, limit);
        iPage = this.sysPetService.selectpage(iPage, sysPetQueryVo);
        return Result.ok(iPage);
    }


    /**
     *
     * @param sysPet
     * @return
     */
    @ApiOperation("添加宠物")
    @PostMapping("/addPet")
    public Result addPet(@ApiParam(value = "宠物信息") @RequestBody SysPet sysPet) {
       boolean b = this.sysPetService.save(sysPet);
        return b ? Result.ok() : Result.fail();
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取宠物")
    @GetMapping("/findSysPetById/{id}")
    public Result findSysPetById(@ApiParam(value = "宠物ID") @PathVariable Long id) {
        SysPet sysPet = this.sysPetService.getById(id);
        return Result.ok(sysPet);
    }


    /**
     *
     * @param sysPet
     * @return
     */
    @ApiOperation("修改宠物信息")
    @PostMapping("/updateSysPet")
    public Result updateSysPet(@ApiParam(value = "宠物信息") @RequestBody SysPet sysPet) {
        boolean b = this.sysPetService.updateById(sysPet);
        return b ? Result.ok() : Result.fail();
    }

    /**
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id去移除一个宠物")
    @DeleteMapping("/removeSysPetById/{id}")
    public Result removeSysPetById(@ApiParam(value = "宠物ID") @PathVariable Long id) {
        boolean b = this.sysPetService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }


}

